//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows.Controls;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;

namespace OpenLS.Spreadsheet.Drawing
{
    public class AbsoluteAnchor : Anchor
    {
        public AbsoluteAnchor(Worksheet worksheet)
            : base(worksheet)
        {

        }
        internal override void ReadXmlCore(ReadContext context)
        {
            using (ReadContext c = context.Read("absoluteAnchor"))
            {
                Point2D pos = new Point2D();
                IOfficeXmlSerializableAs i = pos;
                i.ReadXmlAs("pos", c);
                PositiveSize2D size = new PositiveSize2D();
                i = size;
                i.ReadXmlAs("ext", c);
                ActualWidth = size.Width;
                ActualHeight = size.Height;
                ColumnOffset = pos.X;
                RowOffset = pos.Y;
                Content = ReadContent(c);
                ClientData = c.ReadOptionalElement<AnchorClientData>(AnchorClientData.xmlName);
            }
        }


        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("absoluteAnchor"))
            {
                Point2D pos = new Point2D();
                pos.X = ColumnOffset;
                pos.Y = RowOffset;
                c.WriteElementAs(pos, "pos");
                PositiveSize2D size = new PositiveSize2D();
                size.Width = ActualWidth;
                size.Height = ActualHeight;
                c.WriteElementAs(size, "ext");
                c.WriteElement(Content);
                c.WriteOptionalElement(ClientData);
            }
        }

        double actualWidth;
        public override double ActualWidth
        {
            get
            {
                return actualWidth;
            }
            set
            {
                actualWidth = value;
                if (Content is ShapeBase)
                {
                    ShapeBase b = (ShapeBase)Content;
                    b.Width = value;
                }
                NotifyPropertyChanged("ActualWidth");
            }
        }

        double actualHeight;
        public override double ActualHeight
        {
            get
            {
                return actualHeight;
            }
            set
            {
                actualHeight = value;
                if (Content is ShapeBase)
                {
                    ShapeBase b = (ShapeBase)Content;
                    b.Height = value;
                }

                NotifyPropertyChanged("ActualHeight");
            }
        }
        double rowOffset;
        public override double RowOffset
        {
            get
            {
                return rowOffset;
            }
            set
            {
                rowOffset = value;
                NotifyPropertyChanged("RowOffset");
            }
        }
        double columnOffset;
        public override double ColumnOffset
        {
            get
            {
                return columnOffset;
            }
            set
            {
                columnOffset = value;
                NotifyPropertyChanged("ColumnOffset");
            }
        }


        internal override void DragSideImp(double amount, Dock sideType)
        {
            switch (sideType)
            {
                case Dock.Left:
                    ColumnOffset += amount; break;
                case Dock.Top:
                    RowOffset += amount; break;
                case Dock.Right:
                    ActualWidth += amount; break;
                case Dock.Bottom:
                    ActualHeight += amount; break;
                default:
                    throw new NotSupportedException();
            }

        }

        internal override void DragImp(double x, double y)
        {
            throw new NotImplementedException();
        }
    }
}
